<?php
namespace Admin\Controller;

class NewsController extends Controller {

    public function index() {
        $m    = D2();
        $page = $m->where($this->indexExpr)->order($this->sortExpr)->page2();

        $this->assign('page', $page)
             ->display();
    }

    public function modify() {
        $id = I('get.id', 0, 'intval');

        if (empty($id)) {
            throw new \Exception('参数不完整，无法进行后续操作');
        }

        $m = D2();

        if (!$record = $m->where(['id' => $id])->find()) {
            throw new \Exception('未找到相关数据');
        }

        $this->assign('record', $record)
             ->display();
    }

    public function doSave() {
        $d = I('post.');
        $m = D2();

        try {
            $r = $m->save2($d)
                 ? ['s' => true,  'm' => '保存成功']
                 : ['s' => false, 'm' => '保存出错'];
        } catch(\Exception $e) {
            $r = ['s' => false,  'm' => $e->getMessage()];
        }

        showJson($r);
    }

    public function detail() {
        $this->modify();
    }

    public function del() {
        $id = I('get.id', 0, 'intval');

        if(empty($id)) {
            showJson(['s' => false, 'm' => '参数不完整，无法进行后续操作']);
        }

        $r = D2()->del($id)
             ? ['s' => true,  'm' => '删除成功']
             : ['s' => false, 'm' => '删除出错'];
        

        showJson($r);
    }

}
